<template>
  <div class="app-container">  
      <el-form ref="filterForm" :model="formData" :rules="rules" label-position="left" label-width="100px">
        <el-form-item label="期货名称" prop="pageName">
          <el-input v-model="formData.symbol" clearable placeholder="期货编码" maxlength="20" show-word-limit/>
          <el-input v-model="formData.pageName" clearable placeholder="期货名称" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="交易所" prop="exchangeID">
          <el-select v-model="formData.exchangeID" placeholder="请选择">
            <el-option
              v-for="item in exchangeOptions"
              :key="item.ID"
              :label="item.exchangeName"
              :value="item.ID">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="期货LOGO" prop="pageLogo">
          <Upload v-model="formData.pageLogo" />
        </el-form-item>
        <el-form-item label="最小变动单位" prop="miniChangingUnit">
          <el-input v-model="formData.miniChangingUnit" clearable placeholder="最小变动单位" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="报价单位" prop="quotationUnit">
          <el-input v-model="formData.quotationUnit" clearable placeholder="报价单位" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="交易单位" prop="tradeUnit">
          <el-input v-model="formData.tradeUnit" clearable placeholder="交易单位" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="涨跌停板幅度" prop="riseFallRange">
          <el-input v-model="formData.riseFallRange" clearable placeholder="涨跌停板幅度" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="合约月份" prop="contractMonth">
          <el-input v-model="formData.contractMonth" clearable placeholder="合约月份" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="交易时间" prop="tradeTimes">
          <el-input v-model="formData.tradeTimes" clearable placeholder="交易时间" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="最后交易日" prop="lastTradeDay">
          <el-input v-model="formData.lastTradeDay" clearable placeholder="最后交易日" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="最后交割日" prop="lastDeliveryDay">
          <el-input v-model="formData.lastDeliveryDay" clearable placeholder="最后交割日" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="最低交易保证金" prop="lowestTradeMargin">
          <el-input v-model="formData.lowestTradeMargin" clearable placeholder="最低交易保证金" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="交割地点" prop="deliveryLocation">
          <el-input v-model="formData.deliveryLocation" clearable placeholder="交割地点" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="交割等级" prop="deliveryLevel">
          <el-input v-model="formData.deliveryLevel" clearable placeholder="交割等级" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="上市日期" prop="marketDate">
          <el-date-picker 
          clearable
            v-model="formData.marketDate" placeholder="选择日期"
            type="date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="formData.remark" maxlength="200" show-word-limit clearable placeholder="备注"/>
        </el-form-item>
      </el-form>
      <el-row :gutter="20">
        <el-col :span="14">          
            <el-button type="info" @click="onFormSaveButton()">保存</el-button>
            <el-button type="primary" @click="onFormSaveAndNewButton()">保存并新建</el-button>
            <el-button type="primary" @click="onFormNewButton()">新建</el-button>
        </el-col>
        <el-col :span="6" style="text-align:right;">          
          <el-button type="warning" @click="onFormCannelButton()">取消</el-button>
          <el-button type="danger" v-if="pageID > 0" @click="onFormRemoveButton()">删除</el-button>
        </el-col>
      </el-row>

  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Region from '@/components/Region/RegionSimple'
import Upload from '@/components/Upload/SingleImage3'
import {getpageDetail,savepage,removepage} from '@/api/pagination/page'



export default {
  name: 'pageForm',
  components: { Region,Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      pageID:undefined,
      exchangeOptions:[],
      formData: {
      },
      rules:{
         pageName: [
            { required: true, message: '请输入期货名称', trigger: 'blur' },
            { min: 3, max: 80, message: '长度在 3 到 80 个字符', trigger: 'blur' }
          ],
         establishDate: [
            { required: true, message: '请选择/输入成立日期', trigger: 'blur' },
            { len: 10, message: '日期应为10位', trigger: 'blur' }
          ],
         pageRegAddress: [
            { required: true, message: '请输入期货注册地址', trigger: 'blur' },
            { min: 2, max: 80, message: '长度在 2 到 80 个字符', trigger: 'blur' }
          ],
      }
    }
  },
  created() {
    let pageID = this.$route.query.pageID 
    pageID = parseInt(pageID)
    if(isNaN(pageID) == false){
      this.pageID = pageID
      this.getInfo()
    }
  },
  methods: {
    getInfo:function(){
      let _this = this;
      getpageDetail({
        "pageID":this.pageID
      }).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            this.formData = response.data
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    onFormSaveButton:function(){
      let _this = this;
      this.$refs['filterForm'].validate((valid) => {
        if (valid) {
            
            savepage(_this.formData).then((response) => {
              console.log("getList",response)
                if (response.code === 200) {
                  _this.$message({
                    message: "成功",
                    type: 'error'
                  })
                } else {
                  _this.$message({
                    message: response.msg,
                    type: 'error'
                  })
                }
            })
        } else {
            console.log('error submit!!');
            return false;
        }
      })
    },
    onFormSaveAndNewButton:function(){
      let _this = this;
      savepage(_this.formData).then((response) => {
          if (response.code === 200) {
            _this.onFormNewButton()
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    onFormNewButton:function(){
        this.formData = {

        }
    },
    onFormCannelButton:function(){
      this.$router.push({ path: 'page'})
    },
    onFormRemoveButton:function(){
      let _this = this;
      removepage({
        "pageID":this.pageID
      }).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            _this.onFormCannelButton()
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    }
  }
}
</script>
<style>
</style>